package com.share.netty.im.server;

import com.share.netty.im.codec.PacketDecoder;
import com.share.netty.im.codec.PacketEncoder;
import com.share.netty.im.codec.Spliter;
import com.share.netty.im.server.handler.AuthHandler;
import com.share.netty.im.server.handler.LifecycleTestHandler;
import com.share.netty.im.server.handler.LoginRequestHandler;
import com.share.netty.im.server.handler.MessageRequestHandler;
import com.share.netty.im.server.handler.inbound.InBoundHandlerA;
import com.share.netty.im.server.handler.inbound.InBoundHandlerB;
import com.share.netty.im.server.handler.inbound.InBoundHandlerC;
import com.share.netty.im.server.handler.outbound.OutBoundHandlerA;
import com.share.netty.im.server.handler.outbound.OutBoundHandlerB;
import com.share.netty.im.server.handler.outbound.OutBoundHandlerC;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Date;

/**
 * @author caifan
 * @createTime 2020/5/10
 * @desc:
 */
public class NettyServer {
    private static final int PORT = 9000;

    public static void main(String[] args) {
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(boss, worker)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 1024) //设置容量
                        .childOption(ChannelOption.SO_KEEPALIVE, true) //保持连接
                        .childOption(ChannelOption.TCP_NODELAY, true)   //设置没有延迟
                        .childHandler(new ChannelInitializer<NioSocketChannel>() {
                            @Override
                            protected void initChannel(NioSocketChannel ch) {
                                /*ch.pipeline().addLast(new InBoundHandlerA());
                                ch.pipeline().addLast(new InBoundHandlerB());
                                ch.pipeline().addLast(new InBoundHandlerC());

                                ch.pipeline().addLast(new OutBoundHandlerA());
                                ch.pipeline().addLast(new OutBoundHandlerB());
                                ch.pipeline().addLast(new OutBoundHandlerC());
                                ch.pipeline().addLast(new NettyServerHandler());*/

                                //生命周期测试
//                                ch.pipeline().addLast(new LifecycleTestHandler());

                                ch.pipeline().addLast(new Spliter());
                                ch.pipeline().addLast(new PacketDecoder());
                                ch.pipeline().addLast(new LoginRequestHandler());
                                ch.pipeline().addLast(new AuthHandler());
                                ch.pipeline().addLast(new MessageRequestHandler());
                                ch.pipeline().addLast(new PacketEncoder());


                            }
                        });

        bind(serverBootstrap,PORT);

    }

    /**
     * 绑定监听器
     * @param serverBootstrap
     * @param port
     */
    private static void bind(final ServerBootstrap serverBootstrap, final int port) {
        serverBootstrap.bind(port).addListener(future -> {
            if (future.isSuccess()) {
                System.out.println(new Date() + ": 端口[" + port + "]绑定成功!");
            } else {
                System.err.println("端口[" + port + "]绑定失败!");
            }
        });
    }
}
